import React, { Component } from 'react';

import { Form, Input, Button, Row, Col, message } from 'antd';
import { connect } from 'dva';
import GetYCodeFormItem from '../../components/GetYCodeFormItem';
const FormItem = Form.Item;

@Form.create()
@connect(({ loading }) => ({
  loading: loading.effects['editPhone/setResetPhone'],
}))
class BindNewPhone extends Component {
  handleSubmit = e => {
    e.preventDefault();
    const { form, dispatch, changeCurrent,oldCode } = this.props;
    form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        const newData = {};
        newData.newCode = values.code;
        newData.newMobile = values.phone;
        newData.oldCode = oldCode;
        dispatch({
          type: 'editPhone/setResetPhone',
          payload: newData,
        })
          .then(msg => {
            changeCurrent(2);
          })
          .catch(errMsg => {
            message.error(errMsg);
          });
      }
    });
  };
  render() {
    const {
      form: { getFieldDecorator },
      form,
      loading,
    } = this.props;
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 18 },
      },
    };
    return (
      <Form onSubmit={this.handleSubmit}>
        <Row>
          <Col span={8}>
            <FormItem label="手机号码" {...formItemLayout}>
              {getFieldDecorator('phone', {
                rules: [{ required: true, message: '请输入手机号码!' }],
              })(<Input placeholder="手机号码" />)}
            </FormItem>
          </Col>
        </Row>

        <GetYCodeFormItem
          disabled={loading}
          id="code"
          validField="phone"
          type="phone"
          serviceType={5}
          form={form}
          formItemLayout={formItemLayout}
        />
        <Row>
          <Col span={8} offset={2}>
            <Button type="primary" htmlType="submit" disabled={loading}>
              {loading ? '请稍后' : '下一步'}
            </Button>
          </Col>
        </Row>
      </Form>
    );
  }
}
export default BindNewPhone;
